﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RecipeBookBusiness
{
    public class Rating
    {
        private RecipeBookEntities _entities = new RecipeBookEntities();
        private int _id;
        private int _userID;
        private int _recipeID;
        private int _commentID;
        private bool _isRecipe;
        private int _rating;

        public Rating(int ratingID)
        {
            loadRecipeClass(ratingID);
        }

        public Rating(int userID, int? recipeID, int? commentID, bool isRecipe, int rating)
        {
            try
            {
                RatingDataRow rdr = new RatingDataRow();
                rdr.IDUser = userID;
                rdr.IDRecipe = recipeID;
                rdr.IDComment = commentID;
                rdr.IsRecipe = isRecipe;
                rdr.Rating = rating;

                //Save to DB and get ID
                _entities.AddToRatingDataRowSet(rdr);
                _entities.SaveChanges();
                int newRatingID = 0;
                foreach (RatingDataRow idRdr in _entities.RatingDataRowSet)
                {
                    if (idRdr.ID > newRatingID)
                        newRatingID = idRdr.ID;
                }
                loadRecipeClass(newRatingID);
            }
            catch (Exception ex)
            {
                //TODO: Real exception handling
                Console.Write(ex.Message);
            }
        }

        public void DeleteRating()
        {
            RatingDataRow rdr = (from c in _entities.RatingDataRowSet
                                 where c.ID == _id
                                 select c).FirstOrDefault();
            _entities.DeleteObject(rdr);
            _entities.SaveChanges();            
        }

        private void loadRecipeClass(int ratingID)
        {
            RatingDataRow rdr = (from c in _entities.RatingDataRowSet
                                 where c.ID == ratingID
                                 select c).FirstOrDefault();

            _id = rdr.ID;
            _userID = rdr.IDUser;
            _recipeID = (int)rdr.IDRecipe;
            _commentID = (int)rdr.IDComment;
            _isRecipe = rdr.IsRecipe;
            _rating = rdr.Rating;
        }

        public int ID
        {
            get
            {
                return _id;
            }
        }

        public int UserID
        {
            get
            {
                return _userID;
            }
        }

        public int RecipeID
        {
            get
            {
                return _recipeID;
            }
        }

        public int CommentID
        {
            get
            {
                return _commentID;
            }
        }

        public bool IsRecipe
        {
            get
            {
                return _isRecipe;
            }
        }

        public int NumericRating
        {
            get
            {

                return _rating;
            }
        }
    }
}
